<?php
/**
 * 庆典公司维护
 * 页面初始化处理
 * 联系庆典公司处理
 * 关于庆典公司介绍处理
 * 庆典公司的logo上传
 * 修改密码处理
 * @author 老杨
 *
 */
class WeddingManagerAction extends MY_Controller {
	
	function WeddingManagerAction() {
		parent::MY_Controller('wedding');
	}
	
	/**
	 * 页面初始化
	 */
	function index() {
	
	
		$this->load->view('wedding/index');
	}
	
	/**
	 * 联系庆典公司
	 * TODO 添加google地图 指令地图坐标
	 * 地址 电话 email维护
	 */
	function contact() {
	
			$this->load->helper (array('form'));
	  		$this->load->library ('form_validation');
	  		$this->form_validation->set_rules ('phone', '电话', 'trim|max_length[30]|xss_clean');
	  		$this->form_validation->set_rules ('address', '地址', 'trim|max_length[60]|xss_clean');
	  		if($this->form_validation->run () == false){
				//有错误,返回页面重新执行
			$this->load->view ('wedding/wedding_contact');
			}else{
				//无错误,提交到数据库,并把图片保存到目录下,是修改
				$data = $this->_form_data ();
				unset ($data ['action']);
				unset ($data ['area']);
				$this->load->library('session');
				$wedding_id = $this->session->userdata ('wedding_id');
				$this->load->model ('wedding/weddingmodel', 'weddingModel', true);
				$this->weddingModel->update ($wedding_id, $data);
				//echo "保存成功";
				//保存成功后跳转的代码未实现
			}
	
			$this->load->view ('wedding/wedding_contact');
	
	}
	
	/**
	 * 关于庆典公司简介
	 * 要求-图片大小150k GIF PNG JPG
	 * logo上传
	 */
function info() {
		
			$this->load->helper (array ('form'));
	  		$this->load->library ('form_validation');
	  		$this->form_validation->set_rules ('i_name', '电话', 'trim|max_length[30]|xss_clean');
	  		$this->form_validation->set_rules ('corp_detail', '简介', 'trim|max_length[2000]|xss_clean');
			if($this->form_validation->run () == false){
			//有错误,返回页面重新执行
			$this->load->view('wedding/wedding_info');
			}else{
				$data = $this->_form_data ();
				unset ($data ['action']);
				$this->load->library ('session');
				$wedding_id = $this->session->userdata ('wedding_id');
				//无错误,提交到数据库,并把图片保存到目录下,是修改
				//以下是上传的代码,需要把文件名保存到数据库
				$this->load->library ( 'common' );
				$file_name = $this->common->uuid ();
				//set upload path
				$file_path = $this->_get_wedding_path ();
				$config ['upload_path'] = $_SERVER ['DOCUMENT_ROOT'] . $file_path;
				$config ['allowed_types'] = 'gif|jpg|png';
				$config ['max_size'] = '150';
				$config ['max_width'] = '1024';
				$config ['max_height'] = '768';
				$config ['file_name'] = $file_name;
				$this->load->library ( 'upload', $config );
				if (! $this->upload->do_upload ( 'logo_img' )) {
					$upload_error = $this->upload->display_errors ();
				} else {
						/*上传成功*/
					$info = $this->upload->data ();
					$data ['logo_img'] = $file_path . $info ['orig_name'];
					$data ['name'] = $data ['i_name'];
					unset($data ['i_name']);
					$data ['introduction'] = $data ['corp_detail'];
					unset($data ['corp_detail']);
					$this->load->model ('wedding/weddingmodel', 'weddingModel', true);
					$this->weddingModel->update ($wedding_id, $data);	
				 	}
					//以上是上传的代码,需要把文件名保存到数据库
					
					//修改已经完成,后面的还没设置,修改成功页
				
			}
			
			
			$this->load->helper (array('form'));
	  		$this->load->library ('form_validation');
	  		$this->form_validation->run ();
			$this->load->view ('wedding/wedding_info');
			 
	}
	
	/**
	 * 修改密码
	 */
	function change_password() {
			$this->load->helper ( array ('form', 'url' ) );
			$this->load->library ( 'form_validation' );
			//常规校验
			$this->form_validation->set_rules ('pass_error', '', 'callback__chpw_pass_check');
			$this->form_validation->set_rules ('npass', '密码', 'required|matches[rpass]|xss_clean');
			$this->form_validation->set_rules ('rpass', '密码确认', 'required');
			if ($this->form_validation->run() == FALSE) {
   				//初始化或出现错误的场合进入wedding/chage_password视图
   				$form_error  = validation_errors();
				$this->load->view ('wedding/chg_pw');
				return;
  			}else {
  				//保存更改到数据库呗..
  				$data = array ();
  				$this->load->library ('session');
				$wedding_id = $this->session->userdata ('wedding_id');
  				$this->load->model('wedding/weddingmodel', 'weddingModel', true);
				$data['password'] = md5 (trim ($this->input->post ('npass')));
				$this->weddingModel->update ($wedding_id, $data);
				
  			}
			$this->load->view ('wedding/chg_pw');

	}

function ajax_check_pass(){
	if($_POST){
		$this->load->model ('wedding/weddingmodel', 'weddingModel', true);
		$password = $this->input->post ('pass');
		$this->load->library ('session');
		$id = $this->session->userdata ('wedding_id');
		$query_array = $this->weddingModel->get_one($id);
		$pass_in_db = $query_array ['password'];
		if($pass_in_db != md5(trim($password))){
			echo "原密码不正确";
		}
		
	
	}
}
function _chpw_pass_check(){
	$password = $this->input->post('pass');
	$this->load->model('wedding/weddingmodel', 'weddingModel', true);
	$this->load->library ('session');
	$id = $this->session->userdata ('wedding_id');
	$query_array = $this->weddingModel->get_one($id);
	$pass_in_db = $query_array ['password'];
	if($pass_in_db == md5 (trim ($password))){
			return true;
	}else {
			echo "原密码不正确";
			return false;
	}
	
}
}